/**
 * 提交回复
 */
function commentPost() {

    var questionId = $("#questionId").val();
    var section = $("#commentSection");
    var content = $("#textArea").val();
    commentTarget(questionId,1,content);
}

function commentTarget(targetId,type,content) {
    if(!content){
        alert("输入内容不能为空");
    }else{
        $.ajax({
            type: "POST",
            url: "/comment",
            contentType:"application/json",
            data: JSON.stringify({
                "parentId": targetId,
                "content": content,
                "type": type
            }),
            success: function success(response) {
                if(response.code==200){
                    // section.hide();
                    window.location.reload();
                }else if(response.code==2001){
                    var isTrue = confirm(response.message);
                    if(isTrue){
                        window.localStorage.setItem("isUser",isTrue.toString());
                        window.open("https://github.com/login/oauth/authorize?client_id=9d0c691a446c4c104747&redirect_uri=http://localhost/callback&scope=user&state=1");
                    }
                }else {
                    alert(response.message);
                }
            },
            dataType:"json"
        });
    }
}

/**
 * 二级评论
 *
 */
function  comment(e) {
    var commentId = e.getAttribute("data-id");
    var content =$("#input-"+commentId).val();
    commentTarget(commentId,2,content);
}


/**
 * 展开二级评论
 *
 */
function collapseComments(e) {

    var commentId= e.getAttribute("data-id");
    var comments = $("#comment-"+commentId);

    //获取二级评论的展开状态
    var collapse = e.getAttribute("data-collapse");
    if(collapse){
        //折叠二级评论
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("active");
    } else{
        var subCommentContainer = $("#comment-"+commentId);
        if (subCommentContainer.children().length != 1) {
            //展开二级评论
            comments.addClass("in");
            // 标记二级评论展开状态
            e.setAttribute("data-collapse", "in");
            e.classList.add("active");
        } else {
            $.getJSON("/comment/" + commentId, function (data) {
                $.each(data.data.reverse(), function (index, comment) {
                    var mediaLeftElement = $("<div/>", {
                        "class": "media-left"
                    }).append($("<img/>", {
                        "class": "media-object img-rounded icon-index",
                        "src": comment.user.avatarUrl
                    }));

                    var mediaBodyElement = $("<div/>", {
                        "class": "media-body"
                    }).append($("<h5/>", {
                        "class": "media-heading",
                        "html": comment.user.name
                    })).append($("<div/>", {
                        "html": comment.content
                    })).append($("<div/>", {
                        "class": "menu"
                    }).append($("<span/>", {
                        "class": "pull-right",
                        "html": moment(comment.gmtCreate).format('YYYY-MM-DD')
                    })));

                    var mediaElement = $("<div/>", {
                        "class": "media"
                    }).append(mediaLeftElement).append(mediaBodyElement);

                    var commentElement = $("<div/>", {
                        "class": "col-lg-12 col-md-12 col-sm-12 col-xs-12 comments"
                    }).append(mediaElement);

                    subCommentContainer.prepend(commentElement);
                });
                //展开二级评论
                comments.addClass("in");
                // 标记二级评论展开状态
                e.setAttribute("data-collapse", "in");
                e.classList.add("active");
            });
        }

        }
}

function showSelectTag() {
    $("#select-tag").show();
}

function selectTag(e) {
    var selectTag = e.getAttribute("data-tag");
    var previous = $("#tag").val();
    if(previous.indexOf(selectTag)== -1){
        if(previous){
            $("#tag").val(previous+','+selectTag);
        }else{
            $("#tag").val(selectTag);
        }
    }
}
